#include "test_bit_win_xml.h"

#include "bitwin/rows.h"


using namespace BitWin;


void TestBitWinXml::testColumnTitles_data()
{
    QTest::addColumn<QDomElement>("xml_data");
    QTest::addColumn<QStringList>("titles_expected");
    
    QTest::newRow("empty (at all)")
        << createDoc("<empty_at_all/>") << QStringList()
    ;
    QTest::newRow("empty (no columns section)")
        << createDoc(
            "<empty>"
                "<Foo>"
                    "<Bar/>"
                "</Foo>"
                "<Baz/>"
            "</empty>"
        )
        << QStringList()
    ;
    QTest::newRow("empty (empty columns section)")
        << createDoc(
            "<empty>"
                "<columns/>"
                "<columns/>"
            "</empty>"
        )
        << QStringList()
    ;
    QTest::newRow("single binary column")
        << createDoc(
            "<single_bin>"
                "<columns>"
                    "<bin title='Foo' bits='7:0'/>"
                "</columns>"
            "</single_bin>"
        )
        << (QStringList() << "Foo")
    ;
    QTest::newRow("no title")
        << createDoc(
            "<no_title>"
                "<columns>"
                    "<bin bits='7:0'/>"
                "</columns>"
            "</no_title>"
        )
        << (QStringList() << "")
    ;
    QTest::newRow("no title, hex column")
        << createDoc(
            "<no_title_hex>"
                "<columns>"
                    "<hex/>"
                "</columns>"
            "</no_title_hex>"
        )
        << (QStringList() << "")
    ;
    QTest::newRow("dual binary columns")
        << createDoc(
            "<dual_bin>"
                "<columns>"
                    "<bin title='Foo' bits='7:0'/>"
                    "<bin title='Bar' bits='4:3'/>"
                "</columns>"
            "</dual_bin>"
        )
        << (QStringList() << "Foo" << "Bar")
    ;
    QTest::newRow("binary, hex, dec columns")
        << createDoc(
            "<bin_hex_dec>"
                "<columns>"
                    "<bin title='Foo' bits='7:0'/>"
                    "<hex title='Bar'/>"
                    "<dec title='Baz'/>"
                "</columns>"
            "</bin_hex_dec>"
        )
        << (QStringList() << "Foo" << "Bar" << "Baz")
    ;
    QTest::newRow("multiple column sections")
        << createDoc(
            "<mult_column_sections>"
                "<columns>"
                    "<bin title='Foo' bits='7:0'/>"
                    "<hex title='Bar'/>"
                    "<dec title='Baz'/>"
                "</columns>"
                "<columns>"
                    "<bin title='Bug' bits='3:0'/>"
                "</columns>"
            "</mult_column_sections>"
        )
        << (QStringList() << "Foo" << "Bar" << "Baz" << "Bug")
    ;
}

void TestBitWinXml::testColumnTitles()
{
    QFETCH(QDomElement, xml_data);
    QVERIFY(!xml_data.isNull());
    QFETCH(QStringList, titles_expected);
    
    PColumnList cols = FromXml::makeColumns(xml_data);
    
    VERIFY_TITLES(cols, titles_expected);
}

void TestBitWinXml::testColumnBehaviour_data()
{
    QTest::addColumn<QDomElement>("xml_data");
    QTest::addColumn<int>("role");
    QTest::addColumn<PGenericBitwise>("indata");
    QTest::addColumn<QStringList>("outdata_expected");
    
    QTest::newRow("single bitwise column")
        << createDoc(
            "<single_bitwise>"
                "<columns>"
                    "<bin title='Bar' bits='7:3'/>"
                "</columns>"
            "</single_bitwise>"
        )
        << BITWISE_DATA_ROLE
        << STRING_BITWISE("foo-bar-baz")
        << (QStringList() << "-bar-")
    ;
    QTest::newRow("single bitwise column, one item in range")
        << createDoc(
            "<one_item_range>"
                "<columns>"
                    "<bin title='Bar' bits='5'/>"
                "</columns>"
            "</one_item_range>"
        )
        << BITWISE_DATA_ROLE
        << STRING_BITWISE("foo-b*r-baz")
        << (QStringList() <<   "*"  )
    ;
    QTest::newRow("three bitwise columns")
        << createDoc(
            "<three_bitwise>"
                "<columns>"
                    "<bin title='Foo' bits='2:0'/>"
                    "<bin title='Bar' bits='6:4'/>"
                    "<bin title='Baz' bits='10:8'/>"
                "</columns>"
            "</three_bitwise>"
        )
        << BITWISE_DATA_ROLE
        << STRING_BITWISE("foo-bar-baz")
        << (QStringList() << "foo" << "bar" << "baz")
    ;
    QTest::newRow("bitwise, hex and dec columns, bitwise role")
        << createDoc(
            "<bin_hex_dec>"
                "<columns>"
                    "<bin title='C0' bits='15:8'/>"
                    "<bin title='DE' bits='7:0'/>"
                    "<hex title='hexadecimal'/>"
                    "<dec title='decimal'/>"
                "</columns>"
            "</bin_hex_dec>"
        )
        << BITWISE_DATA_ROLE
        << GenericBitwise::fromIntegral<quint16>(0xC0DE)
        << (
            QStringList()
                << "0000""0011" << "0111""1011"
                << QString() << QString()
        )
    ;
    QTest::newRow("bitwise, hex and dec columns, display role")
        << createDoc(
            "<bin_hex_dec_display>"
                "<columns>"
                    "<bin title='C0' bits='15:8'/>"
                    "<bin title='DE' bits='7:0'/>"
                    "<hex title='hexadecimal'/>"
                    "<dec title='decimal'/>"
                "</columns>"
            "</bin_hex_dec_display>"
        )
        << int(Qt::DisplayRole)
        << GenericBitwise::fromIntegral<quint16>(0xC0DE)
        << (
            QStringList()
                << QString() << QString()
                << "0xc0de" << "49374"
        )
    ;
    QTest::newRow("nonsense in xml")
        << createDoc(
            "<nonsense_xml>"
                "<columns>"
                    "<bin title='Foo' bits=''/>"
                    "<bin title='Foo'/>"
                    "<bin title='Foo' bits='2:0'/>"
                    "<nonsense>"
                        "<bin title='Nonsense' bits='2:0'/>"
                    "</nonsense>"
                    "<bin title='Bar' bits='6:4'/>"
                    "<bin bits='6:4'/>"
                    "<bin title='Baz' bits=':'/>"
                    "<bin title='Baz' bits='10:8'/>"
                "</columns>"
            "</nonsense_xml>"
        )
        << BITWISE_DATA_ROLE
        << STRING_BITWISE("foo-bar-baz")
        << (
            QStringList()
                << QString("")
                << QString("")
                << "foo"
                << "bar"
                << "bar"
                << QString("")
                << "baz"
        )
    ;
}

void TestBitWinXml::testColumnBehaviour()
{
    QFETCH(QDomElement, xml_data);
    QVERIFY(!xml_data.isNull());
    
    PColumnList cols = FromXml::makeColumns(xml_data);
    
    QFETCH(int, role);
    
    PRow row(new WordRow("Row", 0));
    
    QFETCH(PGenericBitwise, indata);
    QFETCH(QStringList, outdata_expected);
    
    QCOMPARE(cols.size(), outdata_expected.size());
    
    for (int i = 0; i < cols.size(); ++i)
        VERIFY_QVARIANT_QSTRING(
            cols[i]->data(role, row.data(), indata),
            outdata_expected[i]
        );
}
